package com.suredy.app.project.ctrl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.Constants;
import com.suredy.app.project.service.ProjectSrv;
import com.suredy.core.ctrl.BaseCtrl;

/**
 * 项目列表控制器
 * 
 * @author VIVID.G
 * @since 2015-7-30
 * @version v0.1
 */
@Controller
@RequestMapping("/project/list")
public class ProjectListCtrl extends BaseCtrl {

	@Autowired
	private ProjectSrv projectSrv;

	@RequestMapping
	public String list(Integer page, Integer pageSize, String search, Model model) {
		page = page == null || page <= 0 ? 1 : page;
		pageSize = pageSize == null || pageSize <= 0 ? Constants.DEFAULT_PAGE_SIZE : pageSize;

		model.addAttribute("data", this.projectSrv.getForListSearch(page, pageSize, search));
		model.addAttribute("count", this.projectSrv.countForListSearch(search));
		model.addAttribute("key", search);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("total", this.projectSrv.totalForListSearch(search));

		return "app/project/list";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public boolean delete(String[] id) {
		if (!this.isProjDeleter())
			return false;

		if (id == null || id.length <= 0)
			return false;

		return this.projectSrv.delete(id) > 0;
	}

}
